﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FileMoveActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property Target As InArgument(Of String)
    Public Property DestinationFolder As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncFileMoveDelegate(ByVal original As String, ByVal target As String) As Boolean


    Public Sub New()
        Me.DisplayName = My.Resources.FileMoveDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim target = context.GetValue(Me.Target)
        Dim dest = context.GetValue(Me.DestinationFolder)
        Dim asyncExecute = New asyncFileMoveDelegate(AddressOf FileMove)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(target, dest, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncFileMoveDelegate)
        Dim moveResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, moveResult)
    End Sub

    Private Function FileMove(ByVal original As String, ByVal target As String) As Boolean

        Dim copyResult = False
        Try
            target = System.IO.Path.Combine(target, System.IO.Path.GetFileName(original))
            My.Computer.FileSystem.MoveFile(original, target)
            copyResult = True
        Catch ex As Exception

        End Try
        Return copyResult
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FileMove Activity
        build.AddCustomAttributes(GetType(FileMoveActivity),
                                              New ToolboxBitmapAttribute(GetType(FileMoveActivity), "FileMove.png"))
        build.AddCustomAttributes(GetType(FileMoveActivity), "Target",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FilepathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileMoveCategory),
                                              New DisplayNameAttribute(My.Resources.FileMoveOriginalFileDisplay),
                                              New DescriptionAttribute(My.Resources.FileMoveOriginalFileDescription))
        build.AddCustomAttributes(GetType(FileMoveActivity), "DestinationFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileMoveCategory),
                                              New DisplayNameAttribute(My.Resources.FileMoveTargetFolderDisplay),
                                              New DescriptionAttribute(My.Resources.FileMoveTargetFolderDescription))
        build.AddCustomAttributes(GetType(FileMoveActivity), "Result",
                                              New CategoryAttribute(My.Resources.FileMoveCategory),
                                              New DisplayNameAttribute(My.Resources.FileMoveResultDisplay),
                                              New DescriptionAttribute(My.Resources.FileMoveResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
